import java.lang.reflect.Array;
import java.util.*;
import java.util.stream.Collectors;

//必须指明需要比较对象的类，不能只写一个T
public class MyComparator implements Comparator<Cat> {

    public static void main(String[] args) {
        Cat[] cats = new Cat[4];
        cats[0] = new Cat(1, "a");
        cats[1] = new Cat(6, "b");
        cats[2] = new Cat(3, "c");
        cats[3] = new Cat(2, "d");
        List<Cat> cats1 = new ArrayList<>();
        cats1 = Arrays.stream(cats).collect(Collectors.toList());//
        Collections.sort(cats1, (o1,o2)->o1.getAge()-o2.getAge());//lambda
        System.out.println(cats1);
        Integer x = 2;
        Integer y = 2;
        System.out.println(x.compareTo(y));
        Arrays.sort(cats, new MyComparator());//@数组排序,默认排名序
        for (Cat c : cats) System.out.println(c);
        System.out.println("----------------");
        MyComparable[] ms = new MyComparable[3];//可比较排序
        ms[0] = new MyComparable(cats[0]);
        ms[1] = new MyComparable(cats[1]);
        ms[2] = new MyComparable(cats[2]);
        Arrays.sort(ms);
        for (MyComparable s : ms) System.out.println(s);


    }


    @Override
    public int compare(Cat o1, Cat o2) {
        /**
         * o1 是 n+1  o2 是 n
         * o1.comp.o2表示小在前
         */
        //compareTo  小于 -1  等于 0   大于 1
        //返回-1 o1在前 不需要移动
        //返回1 结果不变
//        return o1.getAge().compareTo(o2.getAge());
        return o1.getAge().compareTo(o2.getAge());
    }
}
